package cn.ly.checkin.service;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ly.checkin.domain.Image;

@Repository
@Transactional
public class ImagesService {
	
	@Autowired 
	private SessionFactory sessionFactory;
	
	public void storeImage(Image img){
		 Serializable sid= this.sessionFactory.getCurrentSession().save(img);
		 System.out.println(sid);
	}
	public Image load(Long id){
		return (Image) this.sessionFactory.getCurrentSession().get(Image.class, id);
	}
	public List<Image> findAll(){
		return this.sessionFactory.getCurrentSession().createQuery("from Image").list();
	}
	
	public List<Image> findByType(Integer type){
		String hql="from Image where type=:type";
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("type", type);
		List<Image> images=query.list();
		if(null!=images){
			return images;
		}else{
			return null;
		}
	}
	public Image findByPath(String path){
		String hql="from Image where path=:path";
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("path", path);
		return (Image) query.list().get(0);
	}
}
